{
 "metadata": {
  "name": "ExampleBN"
 }, 
 "nbformat": 2, 
 "worksheets": [
  {
   "cells": [
    {
     "cell_type": "code", 
     "collapsed": false, 
     "input": [
      "import numpy as np", 
      "from Factor import *", 
      "from FactorOperations import *", 
      "from PGMcommon import *", 
      "import profile", 
      "from CliqueTree import *", 
      "from CliqueTreeOperations import *", 
      "\"\"\" Lets try and construct a basic Bayesian network based on the Student example given", 
      "in Koller and Friedman on pg. 53 (Figure 3.4)", 
      "", 
      "Made the Student network in samiam software <http://reasoning.cs.ucla.edu/samiam/help/>", 
      "saved the file to /Users/amit/software/pgmPy/Student.net.", 
      "", 
      "Read in matlab like so: studentBN=ConvertNetwork('/Users/amit/software/pgmPy/Student.net')", 
      "Then was able to create the Factor objects like so below:", 
      "\"\"\"", 
      "", 
      "D=Factor( [1], [2], [.6,.4],'Difficulty')", 
      "I=Factor( [5], [2], [.7,.3], 'Intelligence' )", 
      "G=Factor( [2, 5,1], [ 3, 2, 2], [.3,.4,.3,.9,.08,.02,.05,.25,.7,.5,.3,.2], 'Grade', )", 
      "S=Factor( [3,5], [2,2], [.95,.05,.2,.8], 'SAT')", 
      "L=Factor( [4,2], [2,3], [.1,.9,.4,.6,.99,.01], 'Letter' )", 
      "", 
      "", 
      "", 
      "INPUTS=[D,G,S,L,I] # list of Factors that comprise the Bayesian Network", 
      "                   # see StudentBN.jpg", 
      "elimination=[3,5,1,4,2]", 
      "", 
      "#print MaxProductVE(elimination, INPUTS)", 
      "", 
      "", 
      "clique_beliefs=ComputeExactMarginalsBP ( INPUTS)", 
      "for c in clique_beliefs:", 
      "    print c", 
      "    print"
     ], 
     "language": "python", 
     "outputs": [
      {
       "output_type": "stream", 
       "stream": "stderr", 
       "text": [
        "A factor is empty!", 
        "A factor is empty!", 
        "A factor is empty!", 
        "A factor is empty!", 
        "A factor is empty!", 
        "FactorMarginalization:Error, resultant factor has empty scope..."
       ]
      }
     ], 
     "prompt_number": 1
    }, 
    {
     "cell_type": "code", 
     "collapsed": false, 
     "input": [
      "#ComputeMarginal returns a factor representing the marginal of a variable", 
      "print ComputeMarginal([3],[I,S],[])", 
      "print \"----\"", 
      "#compute marignal probability of variable 4, which is Pr(Letter)", 
      "print ComputeMarginal( [4], INPUTS, [])", 
      "", 
      "#Pr(L | Intelligence=smart)", 
      "print ComputeMarginal( [4], INPUTS, [1,1])", 
      "", 
      "#Pr(L | Difficulty=easy, Intelligence=smart)", 
      "print ComputeMarginal( [4], INPUTS, [5,1,1,1])", 
      "", 
      "#Pr(Intelligence=gifted|grade=C)", 
      "print ComputeMarginal([5], INPUTS, [2,3])", 
      "", 
      "#Pr(Intelligence=gifted|letter=weak)", 
      "print ComputeMarginal([5], INPUTS, [4,1])", 
      "", 
      "#Pr(Intelligence=gifted|letter=weak, grade=C)", 
      "print ComputeMarginal([5], INPUTS, [2,3,4,1])", 
      "", 
      "#Pr(Intelligence=gifted|grade=C, SAT=high)", 
      "print ComputeMarginal([5], INPUTS, [2,3,3,2])", 
      "", 
      "#Pr(Difficulty|grade=C)", 
      "print ComputeMarginal([1], INPUTS, [2,3])", 
      "", 
      "#Pr(Difficulty|grade=C, SAT=high)", 
      "print ComputeMarginal([1], INPUTS, [2,3,3,2])", 
      "", 
      "#Pr(Intelligence|grade=C,diffculty=hard", 
      "print ComputeMarginal([5], INPUTS, [2,3,1,2])", 
      "", 
      "#Pr(Intelligence|grade=B)", 
      "print ComputeMarginal([5], INPUTS, [2,2])", 
      "", 
      "#Pr(Intelligence|grade=B, difficulty=hard)", 
      "print ComputeMarginal([5], INPUTS, [2,2,1,2])"
     ], 
     "language": "python", 
     "outputs": [
      {
       "output_type": "stream", 
       "stream": "stdout", 
       "text": [
        "name: None", 
        "var: 3", 
        "card: 2.0", 
        "val: 0.725 0.275", 
        "----", 
        "name: None", 
        "var: 4", 
        "card: 2.0", 
        "val: 0.497664 0.502336", 
        "name: None", 
        "var: 4", 
        "card: 2.0", 
        "val: 0.38344 0.61656", 
        "name: None", 
        "var: 4", 
        "card: 2.0", 
        "val: 0.487 0.513", 
        "name: None", 
        "var: 5", 
        "card: 2.0", 
        "val: 0.921052631579 0.0789473684211", 
        "name: None", 
        "var: 5", 
        "card: 2.0", 
        "val: 0.859977816358 0.140022183642", 
        "name: None", 
        "var: 5", 
        "card: 2.0", 
        "val: 0.921052631579 0.0789473684211", 
        "name: None", 
        "var: 5", 
        "card: 2.0", 
        "val: 0.421686746988 0.578313253012", 
        "name: None", 
        "var: 1", 
        "card: 2.0", 
        "val: 0.370709382151 0.629290617849", 
        "name: None", 
        "var: 1", 
        "card: 2.0", 
        "val: 0.240440020953 0.759559979047"
       ]
      }, 
      {
       "output_type": "stream", 
       "stream": "stdout", 
       "text": [
        "", 
        "name: None", 
        "var: 5", 
        "card: 2.0", 
        "val: 0.890909090909 0.109090909091", 
        "name: None", 
        "var: 5", 
        "card: 2.0", 
        "val: 0.825242718447 0.174757281553", 
        "name: None", 
        "var: 5", 
        "card: 2.0", 
        "val: 0.660377358491 0.339622641509"
       ]
      }
     ], 
     "prompt_number": 5
    }, 
    {
     "cell_type": "code", 
     "collapsed": false, 
     "input": [
      "print ComputeMarginal ( [2], INPUTS, [] ) #compute the marginal probabilities of Grade (A, B,C)"
     ], 
     "language": "python", 
     "outputs": [
      {
       "output_type": "stream", 
       "stream": "stdout", 
       "text": [
        "name: None", 
        "var: 2", 
        "card: 3.0", 
        "val: 0.3325 0.2845 0.383"
       ]
      }
     ], 
     "prompt_number": 3
    }, 
    {
     "cell_type": "code", 
     "collapsed": false, 
     "input": [
      "print ComputeMarginal ( [3], INPUTS, [] ) #compute the marginal probability of SAT score (high, low)"
     ], 
     "language": "python", 
     "outputs": [
      {
       "output_type": "stream", 
       "stream": "stdout", 
       "text": [
        "name: None", 
        "var: 3", 
        "card: 2.0", 
        "val: 0.725 0.275"
       ]
      }
     ], 
     "prompt_number": 2
    }, 
    {
     "cell_type": "code", 
     "collapsed": false, 
     "input": [
      "", 
      "print ComputeMarginal ([5], INPUTS, [] )#compute marginal of Intelligence (smart, gifted)"
     ], 
     "language": "python", 
     "outputs": [
      {
       "output_type": "stream", 
       "stream": "stdout", 
       "text": [
        "name: None", 
        "var: 5", 
        "card: 2.0", 
        "val: 0.7 0.3"
       ]
      }
     ], 
     "prompt_number": 5
    }, 
    {
     "cell_type": "code", 
     "collapsed": false, 
     "input": [
      "print ComputeMarginal ( [1], INPUTS, [] )#compute marginal of Difficulty (easy, hard)"
     ], 
     "language": "python", 
     "outputs": [
      {
       "output_type": "stream", 
       "stream": "stdout", 
       "text": [
        "name: None", 
        "var: 1", 
        "card: 2.0", 
        "val: 0.5 0.5"
       ]
      }
     ], 
     "prompt_number": 6
    }, 
    {
     "cell_type": "code", 
     "collapsed": false, 
     "input": [
      "print ComputeMarginal ( [5], INPUTS, [] ) #compute the marginal for intelligence, given evidence "
     ], 
     "language": "python", 
     "outputs": [
      {
       "output_type": "stream", 
       "stream": "stdout", 
       "text": [
        "name: None", 
        "var: 5", 
        "card: 2.0", 
        "val: 0.7 0.3"
       ]
      }
     ], 
     "prompt_number": 4
    }, 
    {
     "cell_type": "code", 
     "collapsed": false, 
     "input": [
      "import numpy as np", 
      "from Factor import *", 
      "from FactorOperations import *", 
      "from PGMcommon import *", 
      "import profile", 
      "D=Factor( [1], [2], [.5,.5],'Difficulty')", 
      "I=Factor( [5], [2], [.7,.3], 'Intelligence' )", 
      "G=Factor( [2, 5,1], [ 3, 2, 2], [.3,.4,.3,.9,.08,.02,.05,.25,.7,.5,.3,.2], 'Grade', )", 
      "S=Factor( [3,5], [2,2], [.95,.05,.2,.8], 'SAT')", 
      "L=Factor( [4,2], [2,3], [.1,.9,.4,.6,.99,.01], 'Letter' )", 
      "", 
      "INPUTS=[D,G,S,L,I] # list of Factors that comprise the Bayesian Network", 
      "                   # see StudentBN.jpg", 
      "id=IdentityFactor( D)", 
      "print id", 
      "print \"--\"", 
      "print D", 
      "print \"--\"", 
      "print 'identity product: ', FactorProduct( id, D)", 
      "#D.getVal()", 
      "#id.getVal()"
     ], 
     "language": "python", 
     "outputs": [
      {
       "output_type": "stream", 
       "stream": "stdout", 
       "text": [
        "name: Difficulty_identity", 
        "var: 1", 
        "card: 2", 
        "val: 1.0 1.0", 
        "--", 
        "name: Difficulty", 
        "var: 1", 
        "card: 2", 
        "val: 0.5 0.5", 
        "--", 
        "identity product:  name: None", 
        "var: 1", 
        "card: 2.0", 
        "val: 0.5 0.5"
       ]
      }
     ], 
     "prompt_number": 2
    }, 
    {
     "cell_type": "code", 
     "collapsed": false, 
     "input": [
      "#ELIMINATE=[3,5,1,4,2]", 
      "ELIMINATE=[3,5,1,2]", 
      "#X=SumProductVE(3, INPUTS)", 
      "print SumProductVE(ELIMINATE, INPUTS)", 
      "#print reduce(lambda x, y: FactorProduct(x,y), X)"
     ], 
     "language": "python", 
     "outputs": [
      {
       "output_type": "stream", 
       "stream": "stdout", 
       "text": [
        "name: None", 
        "var: 4", 
        "card: 2.0", 
        "val: 0.497664 0.502336"
       ]
      }
     ], 
     "prompt_number": 2
    }, 
    {
     "cell_type": "code", 
     "collapsed": false, 
     "input": [
      "A=Factor( [1], [2], [.4, .6],'A')", 
      "B=Factor([2,1], [2, 2], [.1,.55,.90,.45], 'B|A')", 
      "maxb= FactorMaxMarginalization( B,[2]) ", 
      "print \"===\"", 
      "print A", 
      "print \"===\"", 
      "print B", 
      "print IndexToAssignment(np.arange(4),[2,2])-1", 
      "print \"===\"", 
      "print maxb"
     ], 
     "language": "python", 
     "outputs": [
      {
       "output_type": "stream", 
       "stream": "stdout", 
       "text": [
        "===", 
        "name: A", 
        "var: 1", 
        "card: 2", 
        "val: 0.4 0.6", 
        "===", 
        "name: B|A", 
        "var: 2 1", 
        "card: 2 2", 
        "val: 0.1 0.55 0.9 0.45", 
        "[[ 0.  0.]", 
        " [ 1.  0.]", 
        " [ 0.  1.]", 
        " [ 1.  1.]]", 
        "===", 
        "name: None", 
        "var: 1", 
        "card: 2", 
        "val: 0.55 0.9"
       ]
      }
     ], 
     "prompt_number": 14
    }, 
    {
     "cell_type": "code", 
     "collapsed": false, 
     "input": [
      "print FactorProduct( A, maxb)"
     ], 
     "language": "python", 
     "outputs": [
      {
       "output_type": "stream", 
       "stream": "stdout", 
       "text": [
        "name: None", 
        "var: 1 2", 
        "card: 2.0 2.0", 
        "val: 0.36 0.54 0.22 0.33"
       ]
      }
     ], 
     "prompt_number": 11
    }, 
    {
     "cell_type": "code", 
     "collapsed": false, 
     "input": [], 
     "language": "python", 
     "outputs": [
      {
       "ename": "NameError", 
       "evalue": "name 'B' is not defined", 
       "output_type": "pyerr", 
       "traceback": [
        "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m\n\u001b[0;31mNameError\u001b[0m                                 Traceback (most recent call last)", 
        "\u001b[0;32m/Users/amit/software/pgmPy/PythonNotebooks/<ipython-input-2-10713c18dbb3>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m()\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0mmaxb\u001b[0m\u001b[0;34m=\u001b[0m \u001b[0mFactorMaxMarginalization\u001b[0m\u001b[0;34m(\u001b[0m \u001b[0mB\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;36m2\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", 
        "\u001b[0;31mNameError\u001b[0m: name 'B' is not defined"
       ]
      }
     ], 
     "prompt_number": 2
    }, 
    {
     "cell_type": "code", 
     "collapsed": false, 
     "input": [
      "run 'testFactorMaxMarginal.py'"
     ], 
     "language": "python", 
     "outputs": [
      {
       "output_type": "stream", 
       "stream": "stdout", 
       "text": [
        "[1, 7]", 
        "[3, 2]", 
        "[0.0012075472, 0.018339623, 0.055018868, 0.00030188679, 0.012226415, 0.49516981]", 
        "name: test", 
        "var: 1 7", 
        "card: 3 2", 
        "val: 0.0012075472 0.018339623 0.055018868 0.00030188679 0.012226415 0.49516981", 
        "[7]", 
        "name: None", 
        "var: 1", 
        "card: 3", 
        "val: 0.0012075472 0.018339623 0.49516981", 
        "=====", 
        "name: None", 
        "var: 1 3", 
        "card: 3 2", 
        "val: 0.25 0.05 0.15 0.35 0.07 0.21", 
        "[[ 1.  1.]", 
        " [ 2.  1.]", 
        " [ 3.  1.]", 
        " [ 1.  2.]", 
        " [ 2.  2.]", 
        " [ 3.  2.]]"
       ]
      }
     ], 
     "prompt_number": 1
    }, 
    {
     "cell_type": "code", 
     "collapsed": false, 
     "input": [], 
     "language": "python", 
     "outputs": [
      {
       "ename": "ValueError", 
       "evalue": "need more than 1 value to unpack", 
       "output_type": "pyerr", 
       "traceback": [
        "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m\n\u001b[0;31mValueError\u001b[0m                                Traceback (most recent call last)", 
        "\u001b[0;32m/Users/amit/software/pgmPy/PythonNotebooks/<ipython-input-4-b3170680bc14>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m()\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0mAssignmentToIndex\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mnp\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0marange\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;36m4\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0mnp\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mmatrix\u001b[0m\u001b[0;34m(\u001b[0m \u001b[0;34m[\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;36m2\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;36m2\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m]\u001b[0m \u001b[0;34m)\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", 
        "\u001b[0;32m/Users/amit/software/pgmPy/FactorOperations.pyc\u001b[0m in \u001b[0;36mAssignmentToIndex\u001b[0;34m(A, D)\u001b[0m\n\u001b[1;32m     37\u001b[0m     \u001b[0mD\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0mD\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mflatten\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;36m0\u001b[0m\u001b[0;34m)\u001b[0m \u001b[0;31m#turn array into vector (note that this forces a copy), see http://www.scipy.org/NumPy_for_Matlab_Users#head-fd74115e6798fbf3a628094a55d1cb2b2b5cdd3c\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m     38\u001b[0m     \u001b[0mI\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0mnp\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0marray\u001b[0m\u001b[0;34m(\u001b[0m \u001b[0;34m[\u001b[0m\u001b[0;34m]\u001b[0m \u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m---> 39\u001b[0;31m     \u001b[0;34m(\u001b[0m\u001b[0mnrowA\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0mncolA\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0mnp\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mshape\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mA\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m     40\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m     41\u001b[0m     \u001b[0;32mif\u001b[0m \u001b[0mnrowA\u001b[0m\u001b[0;34m==\u001b[0m \u001b[0;36m1\u001b[0m \u001b[0;32mor\u001b[0m \u001b[0mncolA\u001b[0m \u001b[0;34m==\u001b[0m\u001b[0;36m1\u001b[0m\u001b[0;34m:\u001b[0m \u001b[0;31m#if assginments are 1 row or 1 col\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n", 
        "\u001b[0;31mValueError\u001b[0m: need more than 1 value to unpack"
       ]
      }
     ], 
     "prompt_number": 4
    }, 
    {
     "cell_type": "code", 
     "collapsed": false, 
     "input": [
      "print IndexToAssignment(np.arange(4),[2,2])-1"
     ], 
     "language": "python", 
     "outputs": [
      {
       "output_type": "stream", 
       "stream": "stdout", 
       "text": [
        "[[ 0.  0.]", 
        " [ 1.  0.]", 
        " [ 0.  1.]", 
        " [ 1.  1.]]"
       ]
      }
     ], 
     "prompt_number": 6
    }, 
    {
     "cell_type": "code", 
     "collapsed": false, 
     "input": [
      "A=Factor( [1], [2], [.4, .6],'A')", 
      "B=Factor([2,1], [2, 2], [.1,.55,.90,.45], 'B|A')", 
      "maxb= FactorMaxMarginalization( B,[2]) ", 
      "print maxb", 
      "#print FactorProduct(A, maxb)"
     ], 
     "language": "python", 
     "outputs": [
      {
       "output_type": "stream", 
       "stream": "stdout", 
       "text": [
        "name: None", 
        "var: 1", 
        "card: 2", 
        "val: 0.55 0.9"
       ]
      }
     ], 
     "prompt_number": 8
    }, 
    {
     "cell_type": "code", 
     "collapsed": false, 
     "input": [], 
     "language": "python", 
     "outputs": [
      {
       "output_type": "stream", 
       "stream": "stdout", 
       "text": [
        "[ 0.22  0.54]"
       ]
      }
     ], 
     "prompt_number": 10
    }, 
    {
     "cell_type": "code", 
     "collapsed": false, 
     "input": [
      "run 'testFactorMaxMarginal.py'"
     ], 
     "language": "python", 
     "outputs": [
      {
       "output_type": "stream", 
       "stream": "stdout", 
       "text": [
        "[1, 7]", 
        "[3, 2]", 
        "[0.0012075472, 0.018339623, 0.055018868, 0.00030188679, 0.012226415, 0.49516981]", 
        "name: test", 
        "var: 1 7", 
        "card: 3 2", 
        "val: 0.0012075472 0.018339623 0.055018868 0.00030188679 0.012226415 0.49516981", 
        "[7]", 
        "name: None", 
        "var: 1", 
        "card: 3", 
        "val: 0.0012075472 0.018339623 0.49516981", 
        "=====", 
        "name: None", 
        "var: 1 3", 
        "card: 3 2", 
        "val: 0.25 0.05 0.15 0.35 0.07 0.21", 
        "[[ 1.  1.]", 
        " [ 2.  1.]", 
        " [ 3.  1.]", 
        " [ 1.  2.]", 
        " [ 2.  2.]", 
        " [ 3.  2.]]"
       ]
      }
     ], 
     "prompt_number": 1
    }, 
    {
     "cell_type": "code", 
     "collapsed": false, 
     "input": [
      "import numpy as np", 
      "from Factor import *", 
      "from FactorOperations import *", 
      "from PGMcommon import *", 
      "import profile", 
      "D=Factor( [1], [2], [.6,.4],'Difficulty')", 
      "I=Factor( [5], [2], [.7,.3], 'Intelligence' )", 
      "G=Factor( [2, 5,1], [ 3, 2, 2], [.3,.4,.3,.9,.08,.02,.05,.25,.7,.5,.3,.2], 'Grade', )", 
      "S=Factor( [3,5], [2,2], [.95,.05,.2,.8], 'SAT')", 
      "L=Factor( [4,2], [2,3], [.1,.9,.4,.6,.99,.01], 'Letter' )", 
      "", 
      "INPUTS=[D,G,S,L,I] # list of Factors that comprise the Bayesian Network", 
      "                   # see StudentBN.jpg", 
      "ELIMINATE=[3,5,1,4,2]", 
      "#X=SumProductVE(3, INPUTS)", 
      "", 
      "print MaxProductVE(ELIMINATE, INPUTS)"
     ], 
     "language": "python", 
     "outputs": [
      {
       "output_type": "stream", 
       "stream": "stdout", 
       "text": [
        "[[ 1.]", 
        " [ 2.]", 
        " [ 3.]]", 
        "3.0", 
        "[[ 1.  1.]", 
        " [ 2.  1.]", 
        " [ 1.  2.]", 
        " [ 2.  2.]", 
        " [ 1.  3.]", 
        " [ 2.  3.]]", 
        "[[ 1.  1.]", 
        " [ 2.  1.]", 
        " [ 1.  2.]", 
        " [ 2.  2.]", 
        " [ 1.  3.]", 
        " [ 2.  3.]]", 
        "[[ 1.  1.  1.]", 
        " [ 2.  1.  1.]", 
        " [ 1.  2.  1.]", 
        " [ 2.  2.  1.]", 
        " [ 1.  3.  1.]", 
        " [ 2.  3.  1.]", 
        " [ 1.  1.  2.]", 
        " [ 2.  1.  2.]", 
        " [ 1.  2.  2.]", 
        " [ 2.  2.  2.]", 
        " [ 1.  3.  2.]", 
        " [ 2.  3.  2.]]", 
        "[[ 1.  1.]", 
        " [ 2.  1.]", 
        " [ 1.  2.]", 
        " [ 2.  2.]]", 
        "0.184338"
       ]
      }, 
      {
       "output_type": "stream", 
       "stream": "stderr", 
       "text": [
        "FactorMaxMarginalization: Error, resultant factor has empty scope..."
       ]
      }
     ], 
     "prompt_number": 1
    }, 
    {
     "cell_type": "code", 
     "collapsed": false, 
     "input": [
      "D=Factor( [1], [2], [.6,.4],'Difficulty')", 
      "I=Factor( [5], [2], [.7,.3], 'Intelligence' )", 
      "print FactorProduct(D,I)"
     ], 
     "language": "python", 
     "outputs": [
      {
       "output_type": "stream", 
       "stream": "stdout", 
       "text": [
        "name: None", 
        "var: 1 5", 
        "card: 2.0 2.0", 
        "val: 0.42 0.28 0.18 0.12"
       ]
      }
     ], 
     "prompt_number": 3
    }, 
    {
     "cell_type": "code", 
     "collapsed": false, 
     "input": [
      "from PgmsnpCommon import *", 
      "gPrior=returnGenotypePriorFounderFactor('G',['A','C','T'], 1)", 
      "print gPrior"
     ], 
     "language": "python", 
     "outputs": [
      {
       "output_type": "stream", 
       "stream": "stdout", 
       "text": [
        "AA non-ref homoz", 
        "AC tri-alleleic het", 
        "AG heterzygote", 
        "AT tri-alleleic het", 
        "CC non-ref homoz", 
        "CG heterzygote", 
        "CT tri-alleleic het", 
        "GG homoz reference", 
        "GT heterzygote", 
        "TT non-ref homoz", 
        "name: genotypePrior", 
        "var: 1", 
        "card: 10", 
        "val: 0.0005 1.25e-10 0.001 1.25e-10 0.0005 0.001 1.25e-10 0.9985 0.001 0.0005"
       ]
      }
     ], 
     "prompt_number": 1
    }, 
    {
     "cell_type": "code", 
     "collapsed": false, 
     "input": [
      "IndexToAssignment(np.arange(np.prod([3,2])), [3,2])", 
      "IndexToAssignment(np.arange(np.prod([2,2])), [2,2])", 
      "IndexToAssignment(np.arange(np.prod([3,2,2])),[3,2,2])"
     ], 
     "language": "python", 
     "outputs": [
      {
       "output_type": "pyout", 
       "prompt_number": 9, 
       "text": [
        "array([[ 1.,  1.,  1.],", 
        "       [ 2.,  1.,  1.],", 
        "       [ 3.,  1.,  1.],", 
        "       [ 1.,  2.,  1.],", 
        "       [ 2.,  2.,  1.],", 
        "       [ 3.,  2.,  1.],", 
        "       [ 1.,  1.,  2.],", 
        "       [ 2.,  1.,  2.],", 
        "       [ 3.,  1.,  2.],", 
        "       [ 1.,  2.,  2.],", 
        "       [ 2.,  2.,  2.],", 
        "       [ 3.,  2.,  2.]])"
       ]
      }
     ], 
     "prompt_number": 9
    }, 
    {
     "cell_type": "code", 
     "collapsed": false, 
     "input": [
      "foo=Factor([1,2], [3,2], [.5,.1,.3,.8,0,.9], 'factor1')", 
      "bar=Factor([2,3], [2,2], [.5,.1,.7,.2], 'factor2')", 
      "print foo", 
      "print bar"
     ], 
     "language": "python", 
     "outputs": [
      {
       "output_type": "stream", 
       "stream": "stdout", 
       "text": [
        "name: factor1", 
        "var: 1 2", 
        "card: 3 2", 
        "val: 0.5 0.1 0.3 0.8 0.0 0.9", 
        "name: factor2", 
        "var: 2 3", 
        "card: 2 2", 
        "val: 0.5 0.1 0.7 0.2"
       ]
      }
     ], 
     "prompt_number": 14
    }, 
    {
     "cell_type": "code", 
     "collapsed": false, 
     "input": [
      "baz= FactorProduct(foo,bar)", 
      "np.sum( baz.getVal() )"
     ], 
     "language": "python", 
     "outputs": [
      {
       "output_type": "pyout", 
       "prompt_number": 21, 
       "text": [
        "1.5900000000000001"
       ]
      }
     ], 
     "prompt_number": 21
    }, 
    {
     "cell_type": "code", 
     "collapsed": false, 
     "input": [
      "print FactorMarginalization(baz, [1])", 
      "", 
      "#print FactorSum(foo,bar)"
     ], 
     "language": "python", 
     "outputs": [
      {
       "output_type": "stream", 
       "stream": "stdout", 
       "text": [
        "name: None", 
        "var: 2 3", 
        "card: 2.0 2.0", 
        "val: 0.45 0.17 0.63 0.34"
       ]
      }
     ], 
     "prompt_number": 20
    }, 
    {
     "cell_type": "code", 
     "collapsed": false, 
     "input": [
      "logFoo=LogFactor(foo)", 
      "logBar=LogFactor(bar)", 
      "", 
      "print logFoo", 
      "print logBar"
     ], 
     "language": "python", 
     "outputs": [
      {
       "output_type": "stream", 
       "stream": "stdout", 
       "text": [
        "name: factor1", 
        "var: 1 2", 
        "card: 3 2", 
        "val: -0.69314718056 -2.30258509299 -1.20397280433 -0.223143551314 -18.420680744 -0.105360515658", 
        "name: factor2", 
        "var: 2 3", 
        "card: 2 2", 
        "val: -0.69314718056 -2.30258509299 -0.356674943939 -1.60943791243"
       ]
      }
     ], 
     "prompt_number": 7
    }, 
    {
     "cell_type": "code", 
     "collapsed": false, 
     "input": [
      "fSum= FactorSum(logFoo,logBar)", 
      "fVal=fSum.getVal()", 
      "print fVal", 
      "np.sum ( np.exp( fVal ) )"
     ], 
     "language": "python", 
     "outputs": [
      {
       "output_type": "stream", 
       "stream": "stdout", 
       "text": [
        "[ -1.38629436  -2.99573227  -1.89711998  -2.52572864 -20.72326584", 
        "  -2.40794561  -1.04982212  -2.65926004  -1.56064775  -1.83258146", 
        " -20.03011866  -1.71479843]"
       ]
      }, 
      {
       "output_type": "pyout", 
       "prompt_number": 13, 
       "text": [
        "1.5900000029999997"
       ]
      }
     ], 
     "prompt_number": 13
    }, 
    {
     "cell_type": "code", 
     "collapsed": false, 
     "input": [
      "print ComputeMarginal([5], INPUTS, [2,3,3,2])"
     ], 
     "language": "python", 
     "outputs": [
      {
       "output_type": "stream", 
       "stream": "stdout", 
       "text": [
        "name: None", 
        "var: 5", 
        "card: 2.0", 
        "val: 0.421686746988 0.578313253012"
       ]
      }
     ], 
     "prompt_number": 6
    }, 
    {
     "cell_type": "code", 
     "collapsed": false, 
     "input": [
      "IndexToAssignment(np.arange(np.prod([2,2])), [2,2])"
     ], 
     "language": "python", 
     "outputs": [
      {
       "output_type": "pyout", 
       "prompt_number": 7, 
       "text": [
        "array([[ 1.,  1.],", 
        "       [ 2.,  1.],", 
        "       [ 1.,  2.],", 
        "       [ 2.,  2.]])"
       ]
      }
     ], 
     "prompt_number": 7
    }, 
    {
     "cell_type": "code", 
     "collapsed": false, 
     "input": [
      "IndexToAssignment(np.arange(np.prod([2])), [2])"
     ], 
     "language": "python", 
     "outputs": [
      {
       "output_type": "pyout", 
       "prompt_number": 8, 
       "text": [
        "array([[ 1.],", 
        "       [ 2.]])"
       ]
      }
     ], 
     "prompt_number": 8
    }, 
    {
     "cell_type": "code", 
     "collapsed": true, 
     "input": [
      "import numpy as np", 
      "from Factor import *", 
      "from FactorOperations import *", 
      "from PGMcommon import *", 
      "", 
      "carrier=Factor([1], [2], [.0002, 1-.0002], 'carrier')"
     ], 
     "language": "python", 
     "outputs": [], 
     "prompt_number": 8
    }, 
    {
     "cell_type": "code", 
     "collapsed": true, 
     "input": [
      "result=Factor( [1,2], [2,2], [.95,.05,.01,.99], 'result|carrier')"
     ], 
     "language": "python", 
     "outputs": [], 
     "prompt_number": 9
    }, 
    {
     "cell_type": "code", 
     "collapsed": false, 
     "input": [
      "print ComputeMarginal([1], [carrier,result],[2,1])"
     ], 
     "language": "python", 
     "outputs": [
      {
       "output_type": "stream", 
       "stream": "stdout", 
       "text": [
        "name: None", 
        "var: 1", 
        "card: 2.0", 
        "val: 0.00378636907134 0.996213630929"
       ]
      }
     ], 
     "prompt_number": 15
    }, 
    {
     "cell_type": "code", 
     "collapsed": false, 
     "input": [
      "print carrier"
     ], 
     "language": "python", 
     "outputs": [
      {
       "output_type": "stream", 
       "stream": "stdout", 
       "text": [
        "name: carrier", 
        "var: 1", 
        "card: 2", 
        "val: 0.0002 0.9998"
       ]
      }
     ], 
     "prompt_number": 13
    }, 
    {
     "cell_type": "code", 
     "collapsed": true, 
     "input": [], 
     "language": "python", 
     "outputs": []
    }
   ]
  }
 ]
}